import os

import ddt
from testconfig import config

from framework.basetest import BaseTest


@ddt.ddt()
class HeadSpinAPITests(BaseTest):

    def setUp(self):
        super().setUp(config=config)

    def tearDown(self):
        super().tearDown()

    @ddt.data('iPhoneXS', 'android', 'ios')
    def test_get_available_local_device_config(self, device_name):
        self.logger.info(f'获取{device_name}的本地配置')
        device = self.hs_api.get_device_local_config_by_name(device_name)
        self.logger.info(device)

    @ddt.data('iPhoneXS')
    def test_get_local_device_config_directly(self, device_name):
        self.logger.info(f'获取{device_name}的本地配置')
        device = self.hs_api.get_device_local_config_by_name(device_name, ignore_unavailable=True)
        self.logger.info(device)

    @ddt.data('BrowserChrome')
    def test_get_local_browser_config(self, browser_name):
        self.logger.info(f'获取{browser_name}的本地配置')
        browser = self.hs_api.get_browser_local_config_by_name(browser_name)
        self.logger.info(browser)

    @ddt.data('iPhoneXS')
    def test_get_device_udid(self, device_name):
        self.logger.info(f'获取{device_name}的udid')
        udid = self.hs_api.get_device_udid_by_name(device_name)
        self.logger.info(udid)

    @ddt.data('BrowserChrome')
    def test_get_browser_udid(self, browser_name):
        self.logger.info(f'获取{browser_name}的udid')
        udid = self.hs_api.get_browser_udid_by_name(browser_name)
        self.logger.info(udid)

    def test_list_uploaded_apps(self):
        response = self.hs_api.list_uploaded_app()
        self.assertEqual(200, response.status_code)

    def test_get_appId_by_buildCommit(self):
        app_id = self.hs_api.get_appId_by_BuildCommit(self.iosBuildCommit)
        self.logger.info(f'appId = {app_id}')

    @ddt.data('iPhoneXS')
    def test_install_ios_app(self, device_name):
        self.logger.info(f'获取{device_name}的本地配置')
        udid = self.hs_api.get_device_udid_by_name(device_name)
        app_id = self.hs_api.get_appId_by_BuildCommit(self.iosBuildCommit)
        self.hs_api.uninstall_app(udid, self.bundle, False)
        response = self.hs_api.install_pre_upload_app(udid, app_id, True)
        self.assertEqual(200, response.status_code)
        self.assertEqual(0, response.json()['returncode'])

    @ddt.data('SamSung1', 'SamSung2')
    def test_install_android_app(self, device_name):
        self.logger.info(f'获取{device_name}的本地配置')
        udid = self.hs_api.get_device_udid_by_name(device_name)
        app_id = self.hs_api.get_appId_by_BuildCommit(self.androidBuildCommit)
        self.hs_api.uninstall_app(udid, self.appPackage, True)
        response = self.hs_api.install_pre_upload_app(udid, app_id)
        self.assertEqual(200, response.status_code)
        self.assertEqual(0, response.json()['returncode'])

    def test_3_upload_apk_to_headspin_repo(self):
        file_fullpath = os.path.join(os.path.sep, self.project_path_root, 'apps', self.androidBuildCommit)
        response = self.hs_api.upload_app(file_fullpath, self.androidBuildCommit)
        self.assertEqual(200, response.status_code)

    def test_4_upload_ipa_to_headspin_repo(self):
        file_fullpath = os.path.join(os.path.sep, self.project_path_root, 'apps', self.iosBuildCommit)
        response = self.hs_api.upload_app(file_fullpath, self.iosBuildCommit)
        self.assertEqual(200, response.status_code)

    @ddt.data('SamSung1')
    def test_is_android_device_available(self, device_name):
        udid = self.hs_api.get_device_udid_by_name(device_name)
        is_available = self.hs_api.is_device_available(udid)
        self.logger.info(f'设备 {device_name} is_available => {is_available}')

    @ddt.data('SamSung1')
    def test_get_device_driver_url(self, device_name):
        udid = self.hs_api.get_device_udid_by_name(device_name)
        driver_url = self.hs_api.get_driver_url_for_device(udid)
        self.logger.info(f'设备 {device_name} 的 driver_url => {driver_url}')

    @ddt.data('SamSung1', 'SamSung2')
    def test_get_device_capabilities(self, device_name):
        udid = self.hs_api.get_device_udid_by_name(device_name)
        capabilities = self.hs_api.get_device_capabilities(udid)
        self.logger.info(f'设备 {device_name} 的 capabilities => {capabilities}')

    @ddt.data('SamSung1', 'SamSung2')
    def test_lock_unlock(self, device_name):
        udid = self.hs_api.get_device_udid_by_name(device_name)

        response = self.hs_api.lock_device(udid)
        self.assertEqual(200, response.status_code)

        response = self.hs_api.unlock_device(udid)
        self.assertEqual(200, response.status_code)

    def test_1_clear_android_apk_from_repo(self):
        ret = self.hs_api.clear_upload_app_by_identifier(self.appPackage)
        self.assertTrue(ret)

    def test_2_clear_ios_ipa_from_repo(self):
        ret = self.hs_api.clear_upload_app_by_identifier(self.bundle)
        self.assertTrue(ret)
